package com.upf1sh.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.upf1sh.bean.User;
import com.upf1sh.mapper.UserMapper;
import com.upf1sh.utils.JwtUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class UserService {
    @Resource
    private UserMapper userMapper;

    private String salt = "CauCX2030";


    /**
     * delete User
     *
     * @param username
     * @return
     */
    public int deleteUser(String username) {
        QueryWrapper<User> wrapper = new QueryWrapper<User>().eq("username", username);
        return userMapper.delete(wrapper) == 1 ? 1 : 0;
    }

    public Map<String, String> login(User user, HttpServletRequest request) throws UnsupportedEncodingException {

        User dbuser = userMapper.findByUN(user.getUsername());
        String ip;

            //获得真实ip
            ip = request.getHeader("x-forwarded-for") == null ? request.getRemoteAddr() : request.getHeader("x-forwarded-for");
            //更新ip
            userMapper.changeIp(ip, dbuser.getUsername());
            //添加在线状态
            userMapper.changeIsOnline("在线", user.getUsername());
            String token = JwtUtils.generateToken(user.getUsername());
            int isAdmin = dbuser.getIsadmin();
            Map<String, String> tokenAndAuthority = new HashMap<String, String>();
            tokenAndAuthority.put("token", token);
            tokenAndAuthority.put("isAdmin", String.valueOf(isAdmin));
            return tokenAndAuthority;

    }

    /**
     * 2023/4/27 21:02
     * 所有
     *
     * @return
     */
    public List<User> selectOUser() {
        QueryWrapper<User> wrapper = new QueryWrapper<User>()
                .eq("isadmin", 0);
        return userMapper.selectList(wrapper);
    }

    public List<User> selectAdmin() {
        QueryWrapper<User> wrapper = new QueryWrapper<User>()
                .eq("isadmin", 1);
        return userMapper.selectList(wrapper);
    }

    public List<User> selectOnlineUser() {
        QueryWrapper<User> wrapper = new QueryWrapper<User>()
                .eq("isonline", "在线");
        return userMapper.selectList(wrapper);
    }

    public int addUser(String username, String password, String phoneNumber, String email) throws
            UnsupportedEncodingException {
        if (userMapper.findByUN(username) == null) {
            User addUser = new User();
            addUser.setEmail(email);
            addUser.setUsername(username);
            addUser.setIsadmin(0);
            addUser.setIsonline("离线");
            addUser.setPhonenumber(phoneNumber);
            //md5密码加密
            String encodedPassword = DigestUtils.md5DigestAsHex((salt + salt + password + salt + salt).getBytes("UTF-8"));
            addUser.setPassword(encodedPassword);
            addUser.setIcon("http://116.204.81.67:8090/data/file/d885f9c8afc140f89b7053c570889fdb0.png");
            if (userMapper.insert(addUser) == 1) {
                return 1;
            } else return 0;
        } else return 2;
    }

    /**
     * 管理员退出用户
     * 2023/4/27 21:41
     *
     * @param username
     * @return
     */
    public int quitUser(String quit, String username) {
        return userMapper.quitUser(quit, username);
    }

    /**
     * 添加管理员
     * 2023/4/29 10:32
     * UpF1sh
     *
     * @param username
     * @param password
     * @param phoneNumber
     * @param email
     * @return
     */
    public int addAdmin(String username, String password, String phoneNumber, String email) throws
            UnsupportedEncodingException {
        System.out.println("admin" + password);
        if (userMapper.findByUN(username) == null) {
            User addUser = new User();
            addUser.setEmail(email);
            addUser.setUsername(username);
            addUser.setIsadmin(1);
            addUser.setIsonline("离线");
            addUser.setPhonenumber(phoneNumber);
            //md5密码加密
            String encodedPassword = DigestUtils.md5DigestAsHex((salt + salt + password + salt + salt).getBytes("UTF-8"));
            addUser.setPassword(encodedPassword);
            addUser.setIcon("http://116.204.81.67:8090/data/file/d885f9c8afc140f89b7053c570889fdb0.png");
            if (userMapper.insert(addUser) == 1) {
                return 1;
            } else return 0;
        } else return 2;
    }


}
